package cn.shixiaoyu.demo.util;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JdbcStuUtil {
    private static Properties properties = null;
    Connection connection =null;
    Statement statement = null;

    static {
        properties = new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            Class.forName(properties.getProperty("jdbcmysql"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //加载jdbc驱动
    public Statement jdbcStatement() {

        try {
             connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password"));
            statement = connection.createStatement();
            return statement;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //关闭
    public void closeJdbc( ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
